/*
 * Creato da SharpDevelop.
 * Utente: NBasile
 * Data: 14/06/2007
 * Ora: 9.21
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using ABasile;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace webpman
{	
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public class rss : Page
	{	
		//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		#region Data

		string 					EntityDeclaration = "";
		string 					myDescription = "";
		string 					myXML = "";		
		IDataReader 			myReader = null;
		DBManager				DBMan;
		Utility					UMan = new Utility();
		string					rssRequest = "";
		string					rssValue = "";
		string					rssid = "";
		string					rssengine_name = "";
		string					rssengine_mail = "";
		string					rssengine_desc = "";
		string					rssengine_title = "";
		bool					setTitle = false;
		
		#endregion
		//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		#region Page Init & Exit (Open/Close DB connections here...)

		protected void PageInit(object sender, EventArgs e)
		{
		}
		//----------------------------------------------------------------------
		protected void PageExit(object sender, EventArgs e)
		{
		}

		#endregion
		//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		#region Page Load		
		private void Page_Load(object sender, EventArgs e)
		{
			
			if (Request.QueryString["o"] != null)
				if (Request.QueryString["o"] != "")
					rssRequest = Request.QueryString["o"].ToString();
			
			if (Request.QueryString["id"] != null)
				if (Request.QueryString["id"] != "")
					rssValue = Request.QueryString["id"].ToString();
			
			if (Request.QueryString["rssid"] != null)
				if (Request.QueryString["rssid"] != "")
					rssid = Request.QueryString["rssid"].ToString();
			
			if (rssRequest == "" || rssValue == "" || rssid == "")
				return ;
			
			rssengine_name = System.Web.Configuration.WebConfigurationManager.AppSettings["rssengine_name"];
			rssengine_mail = System.Web.Configuration.WebConfigurationManager.AppSettings["rssengine_mail"];
			rssengine_desc = System.Web.Configuration.WebConfigurationManager.AppSettings["rssengine_desc"];
			rssengine_title = System.Web.Configuration.WebConfigurationManager.AppSettings["rssengine_title"];
			
			try
			{
				DBMan = new DBManager(
					Server.MapPath(
						System.Web.Configuration.WebConfigurationManager.AppSettings["dbpath"]));
			}
			catch (Exception Ex)
			{
				UMan.AppendTraceFile("rss.aspx.cs", "Page_Load", Ex.Message);
				return;
			}
			
			
			if(DBMan.CheckRssId(rssid) == "")
				return ;
			
			Session.LCID = 1033;
			Response.ContentType="application/rss+xml";
			Response.Charset = "iso-8859-1";
			
			EntityDeclaration += "<!DOCTYPE rss [" + Environment.NewLine;
			
			// ISO Latin 2
			EntityDeclaration += "<!ENTITY iso-lat2.ent PUBLIC \"ISO 8879:1986//ENTITIES Added Latin 2//EN//XML\" " + Environment.NewLine;
			EntityDeclaration += "\"http://" + Request.ServerVariables["HTTP_HOST"].ToString() + "/entity/iso-lat2.ent\">" + Environment.NewLine;
			EntityDeclaration += " " + Environment.NewLine  + Environment.NewLine;

			// ISO Latin 1
			EntityDeclaration += "<!ENTITY iso-lat1.ent PUBLIC \"ISO 8879:1986//ENTITIES Added Latin 1//EN//XML\" "  + Environment.NewLine;
			EntityDeclaration += "\"http://" + Request.ServerVariables["HTTP_HOST"].ToString() + "/entity/iso-lat1.ent\">"  + Environment.NewLine;
			EntityDeclaration += " "  + Environment.NewLine  + Environment.NewLine;

			// ISO Numeric and Special Graphic
			EntityDeclaration += "<!ENTITY iso-num.ent PUBLIC \"ISO 8879:1986//ENTITIES Numeric and Special Graphic//EN//XML\" "  + Environment.NewLine;
			EntityDeclaration += "\"http://" + Request.ServerVariables["HTTP_HOST"].ToString() + "/entity/iso-num.ent\">"  + Environment.NewLine;
			EntityDeclaration += " "  + Environment.NewLine  + Environment.NewLine;

			// ISO Publishing
			EntityDeclaration += "<!ENTITY iso-pub.ent PUBLIC \"ISO 8879:1986//ENTITIES Publishing//EN//XML\" " + Environment.NewLine;
			EntityDeclaration += "\"http://" + Request.ServerVariables["HTTP_HOST"].ToString() + "/entity/iso-pub.ent\">"  + Environment.NewLine;
			EntityDeclaration += " " + Environment.NewLine  + Environment.NewLine;

			// ISO General Technical
			EntityDeclaration += "<!ENTITY iso-tech.ent PUBLIC \"ISO 8879:1986//ENTITIES General Technical//EN//XML\" "  + Environment.NewLine;
			EntityDeclaration += "\"http://" + Request.ServerVariables["HTTP_HOST"].ToString() + "/entity/iso-tech.ent\">"  + Environment.NewLine;
			EntityDeclaration += " "  + Environment.NewLine  + Environment.NewLine;

			EntityDeclaration += "]>";

			// Header of XML
			myXML = "";
			myXML += "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>" + Environment.NewLine;
			myXML += EntityDeclaration  + Environment.NewLine;
			myXML += "<rss version=\"2.0\">"  + Environment.NewLine;
			myXML += "<channel>"  + Environment.NewLine;
			myXML += "<title><!-- rssengine_title --></title>"  + Environment.NewLine;
			myXML += "<managingEditor>" + rssengine_mail + "</managingEditor>"  + Environment.NewLine;			
			myXML += "<description><![CDATA[" + rssengine_desc + "]]></description>"  + Environment.NewLine;
			myXML += "<link>http://" + Request.ServerVariables["HTTP_HOST"].ToString() + "</link>"  + Environment.NewLine;
			myXML += "<language>en</language>"  + Environment.NewLine;
			myXML += "<generator>" + rssengine_name + "</generator>" + Environment.NewLine;
			
			
			
			if (rssRequest == "project")
			{
				myReader = DBMan.GetRssData(int.Parse(rssValue), "project");		
				
				#region project data loop 
				
				try
				{
					while (myReader.Read())
				    {
						// Set RSS Title
						if (!setTitle)
						{	
							setTitle = true;
							rssengine_title += "PROJECT: " + (string)myReader["PROJECT"];
							myXML = myXML.Replace("<!-- rssengine_title -->", rssengine_title);
						}
						
						//UMan.AppendTraceFile("", "", myReader["NAME"].ToString());	
						myDescription = "";
						myDescription += "<div align=\"left\">" + Environment.NewLine;
						myDescription += "<img border=\"0\" src=\"http://" + Request.ServerVariables["HTTP_HOST"].ToString() + "/webpman-rss.png\"/>" + Environment.NewLine;
						myDescription += "<br/><br/>" + Environment.NewLine;
						myDescription += "<b>Description</b>: " + myReader["DESCRIPTION"].ToString() + "<br/>" + Environment.NewLine;
						myDescription += "<b>Task</b>: " + myReader["TASK"].ToString() + "<br/>" + Environment.NewLine;
						myDescription += "<b>Task State</b>: " + myReader["STATE"].ToString() + "<br/>" + Environment.NewLine;
						myDescription += "<b>User</b>: " + myReader["USER"].ToString() + "<br/>" + Environment.NewLine;
						myDescription += "</div>";
				
						myXML += "<item>" + Environment.NewLine;
						myXML += "<title><![CDATA[" + myReader["NAME"].ToString() + "]]></title>" + Environment.NewLine;					
						myXML += "<description><![CDATA[" + myDescription + "]]></description>" + Environment.NewLine;
						myXML += "<author>" + rssengine_mail + " (" + rssengine_name + ")</author>" + Environment.NewLine;
						myXML += "<pubDate>" + 
							return_RFC822_Date(DateTime.Parse((string)myReader["DATEINS"]) , "GMT")
							+ "</pubDate>" + Environment.NewLine;
						myXML += "<link><![CDATA[http://" + Request.ServerVariables["HTTP_HOST"].ToString() + "/default.aspx?object=task&pid=" + rssValue + "]]></link>" + Environment.NewLine;
						myXML += "<guid>~/rss.aspx?o=project_id=" + myReader["ID"].ToString() + "</guid>" + Environment.NewLine;
						myXML += "</item>" + Environment.NewLine;
				    }
				}
				catch(Exception Ex)
				{
					throw Ex;	
				}				
				
				#endregion project data loop 
				
			  	myReader.Close();
			    myReader.Dispose();
				myReader = null;
			
			
			}
			else if (rssRequest == "task") 
			{
				myReader = DBMan.GetRssData(int.Parse(rssValue), "task");		
				
				#region task data loop 
				
				try
				{
					while (myReader.Read())
				    {
						
						// Set RSS Title
						if (!setTitle)
						{	
							setTitle = true;
							rssengine_title += "TASK: " + (string)myReader["TASK"];
							myXML = myXML.Replace("<!-- rssengine_title -->", rssengine_title);
						}

						
						//UMan.AppendTraceFile("", "", myReader["NAME"].ToString());	
						myDescription = "";
						myDescription += "<div align=\"left\">" + Environment.NewLine;
						myDescription += "<img border=\"0\" src=\"~/webpman-rss.png\"/>" + Environment.NewLine;
						myDescription += "<br/><br/>" + Environment.NewLine;
						myDescription += "<b>Description</b>: " + myReader["DESCRIPTION"].ToString() + "<br/>" + Environment.NewLine;
						myDescription += "<b>Task</b>: " + myReader["TASK"].ToString() + "<br/>" + Environment.NewLine;
						myDescription += "<b>Task State</b>: " + myReader["STATE"].ToString() + "<br/>" + Environment.NewLine;
						myDescription += "<b>User</b>: " + myReader["USER"].ToString() + "<br/>" + Environment.NewLine;
						myDescription += "</div>";
				
						myXML += "<item>" + Environment.NewLine;
						myXML += "<title><![CDATA[" + myReader["NAME"].ToString() + "]]></title>" + Environment.NewLine;					
						myXML += "<description><![CDATA[" + myDescription + "]]></description>" + Environment.NewLine;
						myXML += "<author>" + rssengine_mail + " (" + rssengine_name + ")</author>" + Environment.NewLine;
						myXML += "<pubDate>" + 
							return_RFC822_Date(DateTime.Parse((string)myReader["DATEINS"]) , "GMT")
							+ "</pubDate>" + Environment.NewLine;
						myXML += "<link><![CDATA[http://" + Request.ServerVariables["HTTP_HOST"].ToString() + "/default.aspx?object=item&pid=" + rssValue + "]]></link>" + Environment.NewLine;
						myXML += "<guid>~/rss.aspx?o=task_id=" + myReader["ID"].ToString() + "</guid>" + Environment.NewLine;
						myXML += "</item>" + Environment.NewLine;
				    }
				}
				catch(Exception Ex)
				{
					throw Ex;	
				}				
				
				#endregion task data loop 
				
			  	myReader.Close();
			    myReader.Dispose();
				myReader = null;
			}				
			
			
			
			
			// Footer of XML
			myXML += "</channel>" + Environment.NewLine;
			myXML += "</rss>" + Environment.NewLine;
			
			Response.Write(myXML);
			
	
		}
		#endregion
		//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		#region More...
		#endregion
		//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		#region Initialize Component

		protected override void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}
		//----------------------------------------------------------------------
		private void InitializeComponent()
		{
			this.Load	+= new System.EventHandler(Page_Load);
			this.Init   += new System.EventHandler(PageInit);
			this.Unload += new System.EventHandler(PageExit);  
			
		}
		#endregion
		//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<	
		//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		#region Other Functions
		private string return_RFC822_Date(DateTime myDate, string offset)
		{
			//UMan.AppendTraceFile("rss.aspx.cs", "return_RFC822_Date", myDate.ToString("yyyy-MM-dd HH:mm:ss"));
			
			string myDay = myDate.ToString("ddd");
			string myDays = myDate.Day.ToString();			
			string myMonth = myDate.ToString("MMM");
			string myYear = myDate.Year.ToString();
			string myHours = myDate.Hour.ToString("D2");
			string myMinutes = myDate.Minute.ToString("D2");
			string mySeconds = myDate.Second.ToString("D2");
		
			return 	myDay + ", " +
			       	myDays + " " +
			       	myMonth + " " +
					myYear + " " +
			        myHours + ":" +
			        myMinutes + ":" +
			        mySeconds + " " + 
			        offset;
		}

	
		#endregion
		//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		
		
		
	}

}